package com.leviassaf.androidbasics;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

//45
public class Data extends Activity implements OnClickListener {
	
	EditText edtSend;
	Button btnStartActivity;
	Button btnStartActivityForResult;
	TextView txtGot;
	Intent intent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.get);
		setJavaRefs();

		this.setTitle(String.format("%s-%s", this.getTitle().toString(), this.getClass().getSimpleName()));
	}

	private void setJavaRefs() {
		// TODO Auto-generated method stub
		edtSend = (EditText)findViewById(R.id.edtSend);
		btnStartActivity = (Button)findViewById(R.id.btnStartActivity);
		btnStartActivityForResult = (Button)findViewById(R.id.btnStartActivityForResult);
		txtGot = (TextView)findViewById(R.id.txtGot);
		btnStartActivity.setOnClickListener(this);
		btnStartActivityForResult.setOnClickListener(this);
		
	}

	//47
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		intent = new Intent(this, OpenedClass.class);
		switch (v.getId()){
		case R.id.btnStartActivity:
			String strTextToSendToOtherClass = edtSend.getText().toString();
			Bundle bndl = new Bundle();
			bndl.putString("key", strTextToSendToOtherClass);
			intent.putExtras(bndl);
			startActivity(intent);
			break;
		case R.id.btnStartActivityForResult:
			startActivityForResult(intent, 0);
			break;
		}
	}

	//49
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK){
			Bundle bndl = data.getExtras();
			String str = bndl.getString("answer");
			txtGot.setText(str);
		}
	}
}
